package com.example.laundry.controller;

import com.example.laundry.entity.LaundryOrderEntity;
import com.example.laundry.repository.LaundryOrderRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/laundry")
public class LaundryOrderController {

    @Autowired
    private LaundryOrderRepository laundryOrderRepository;

    // 获取所有洗衣订单
    @GetMapping("/orders")
    public List<LaundryOrderEntity> getAllOrders() {
        return laundryOrderRepository.findAll();
    }

    // 创建新的洗衣订单
    @PostMapping("/orders")
    public LaundryOrderEntity createOrder(@RequestBody LaundryOrderEntity order) {
        return laundryOrderRepository.save(order);
    }

    // 获取特定洗衣订单
    @GetMapping("/orders/{id}")
    public LaundryOrderEntity getOrderById(@PathVariable Long id) {
        return laundryOrderRepository.findById(id).orElse(null);
    }

    // 更新洗衣订单
    @PutMapping("/orders/{id}")
    public LaundryOrderEntity updateOrder(@PathVariable Long id, @RequestBody LaundryOrderEntity orderDetails) {
        return laundryOrderRepository.findById(id).map(order -> {
            order.setCustomerName(orderDetails.getCustomerName());
            order.setCustomerPhone(orderDetails.getCustomerPhone());
            order.setPickupAddress(orderDetails.getPickupAddress());
            order.setDeliveryAddress(orderDetails.getDeliveryAddress());
            order.setTotalAmount(orderDetails.getTotalAmount());
            // You might also want to update the status, but that might require different logic
            // order.setStatus(orderDetails.getStatus());
            return laundryOrderRepository.save(order);
        }).orElse(null);
    }

    // 删除洗衣订单
    @DeleteMapping("/orders/{id}")
    public void deleteOrder(@PathVariable Long id) {
        laundryOrderRepository.deleteById(id);
    }
}